<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2017-11-15 13:21:06
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_dashang extends discuz_table
{
    public function __construct()
    {
        $this->_table         = 'jamesonread_dashang';
        $this->_pk            = 'id';
        $this->_pre_cache_key = 'jamesonreaddashang_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    // 总计打赏收入
    public function allprice($authorid)
    {
        $cacheid = 'allprice-'.$authorid;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $total = (int) DB::result_first('SELECT sum(price) FROM %t WHERE authorid=%d', array($this->_table, $authorid));
        $this->store_cache($cacheid, $total);
        return $total;
    }
    public function count_by_authorid($authorid)
    {
        $cacheid = 'count_by_authorid-'.$authorid;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $total = (int) DB::result_first('SELECT count(id) FROM %t WHERE authorid=%d', array($this->_table, $authorid));
        $this->store_cache($cacheid, $total);
        return $total;
    }
    public function count_by_uid($uid)
    {
        $cacheid = 'count_by_uid-'.$uid;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $count = (int) DB::result_first('SELECT count(id) FROM %t WHERE uid=%d', array($this->_table, $uid));
        $this->store_cache($cacheid, $count);
        return $count;
    }
    public function count_by_bookid($book_id,$type='count')
    {
        $cacheid = "dashang-".$type;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        if($type=='count'){
            $count = (int) DB::result_first('SELECT count(id) FROM %t WHERE book_id=%d', array($this->_table, $book_id));
        }else if($type=='dashang'){
            $count = (int) DB::result_first('SELECT SUM(price) FROM %t WHERE book_id=%d', array($this->_table, $book_id));
        }
        $this->store_cache($cacheid, $count);
        return $count;
    }
    public function fetch_by_authorid($authorid, $start, $size)
    {
        $cacheid = 'fetch_by_authorid-'.$authorid.'-'.$start.'-'.$size;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = (array) DB::fetch_all('SELECT d.uid,d.authorid,d.addtime,d.book_id,d.price,d.username,b.book_name FROM %t as d INNER JOIN %t as b ON d.book_id=b.book_id WHERE d.authorid=%d ORDER BY addtime DESC LIMIT %d,%d', array($this->_table, 'jamesonread_books', $authorid, $start, $size));
        $this->store_cache($cacheid, $data);
        return $data;
    }
    public function fetch_by_uid($uid, $start, $size)
    {
        $cacheid = 'fetch_by_uid-'.$uid.'-'.$start.'-'.$size;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = (array) DB::fetch_all('SELECT d.authorid,d.addtime,d.book_id,d.price,d.username,b.book_name,b.image,b.author,b.uid FROM %t as d INNER JOIN %t as b ON d.book_id=b.book_id WHERE d.uid=%d ORDER BY addtime DESC LIMIT %d,%d', array($this->_table, 'jamesonread_books', $uid, $start, $size));
        $this->store_cache($cacheid, $data);
        return $data;
    }
    function fetch_paihang($book_id,$size=10){
        $cacheid = 'fetch_paihang-'.$book_id.'-'.$size;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = DB::fetch_all('SELECT sum(price) as total,username,addtime,book_id,uid FROM %t WHERE book_id=%d GROUP BY uid ORDER BY total DESC LIMIT %d',array($this->_table,$book_id,$size));
        $this->store_cache($cacheid, $data);
        return $data;
    }
}